package com.example.websocketdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.IOException;
import java.time.LocalDateTime;

@EnableScheduling
@SpringBootApplication
public class WebsocketDemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(WebsocketDemoApplication.class, args);
  }

  @Autowired
  private SimpMessagingTemplate simpMessagingTemplate;

  /**
   * 服务端，像订阅了 /topic/hello 的连接端主动发送消息
   *
   * @throws IOException
   */
  @Scheduled(cron = "0/10 * * * * ?")
  public void sc1() {
    String rspMsg = Thread.currentThread().getName() + " 自动返回 | sc1：" + LocalDateTime.now();
    // 后端主动给前端发送消息
    simpMessagingTemplate.convertAndSend("/topic/hello", rspMsg);
  }
}
